package icu.groot.alan.servlet.report;

import com.fasterxml.jackson.databind.ObjectMapper;
import icu.groot.alan.entity.Report;
import icu.groot.alan.entity.TokenEntity;
import icu.groot.alan.service.ReportService;
import icu.groot.alan.service.impl.ReportServiceImpl;
import icu.groot.alan.util.R;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/report/add.do")
public class ReportAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String announceId = req.getParameter("announceId");
        String temperature = req.getParameter("temperature");
        String oneselfHealth = req.getParameter("oneselfHealth");
        String oneselfHealthDetails = req.getParameter("oneselfHealthDetails");
        String address = req.getParameter("address");
        String route = req.getParameter("route");
        String familyHealth = req.getParameter("familyHealth");
        String familyHealthDetails = req.getParameter("familyHealthDetails");
        ObjectMapper objectMapper = new ObjectMapper();
        String userCode = ((TokenEntity)req.getAttribute("tokenEntity")).getCode();
        String anomaly = "健康";


        try {
            if (StringUtils.isAnyBlank(announceId, temperature, oneselfHealth, oneselfHealthDetails, address, familyHealth, familyHealthDetails, userCode)){
                resp.getWriter().print(objectMapper.writeValueAsString(R.error("参数不全")));
                return;
            }
            //检查是否提交
            ReportServiceImpl reportService = new ReportServiceImpl();
            if (reportService.checkAdd(Integer.parseInt(userCode), Integer.parseInt(announceId)) > 0) {
                resp.getWriter().print(objectMapper.writeValueAsString(R.error("不能重复提交")));
                return;
            }
            if ((!oneselfHealth.equals("健康") && !oneselfHealth.equals("异常")) || (!familyHealth.equals("健康") && !familyHealth.equals("异常"))){
                resp.getWriter().print(objectMapper.writeValueAsString(R.error("参数异常")));
                return;
            }
            if (oneselfHealth.equals("健康")) oneselfHealthDetails = "健康"; else anomaly = "异常";
            if (familyHealth.equals("健康")) familyHealthDetails = "健康"; else anomaly = "异常";
            resp.getWriter().print(objectMapper.writeValueAsString(reportService.add(Integer.parseInt(userCode), Integer.parseInt(announceId), Double.parseDouble(temperature), oneselfHealth, oneselfHealthDetails, route, address, familyHealth, familyHealthDetails, anomaly)));
        }catch (Exception e){
            e.printStackTrace();
            resp.getWriter().print(objectMapper.writeValueAsString(R.error("系统异常")));
        }
    }
}
